package com.cbgolf.oa.views;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cbgolf.oa.R;
import com.cbgolf.oa.base.BaseView;
import com.cbgolf.oa.util.AnimaUtil;
import com.cbgolf.oa.util.ErrorUtil;
import com.cbgolf.oa.util.ScreenUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.util.ViewUtils;

import com.cbgolf.oa.widget.autolayout.utils.AutoUtil;
import org.xutils.view.annotation.ViewInject;

/**
 * @author lq
 * @date 2017/11/4
 */

public class ErrorView extends BaseView {

  @ViewInject(R.id.g_error_ll)
  private View errorView;
  @ViewInject(R.id.g_error_layout_ll)
  private View layout;
  @ViewInject(R.id.g_error_reload_tv)
  private TextView reLoadTv;
  @ViewInject(R.id.g_error_loading_tv)
  private TextView loadingTv;
  @ViewInject(R.id.g_error_logo_iv)
  private ImageView logoIv;
  @ViewInject(R.id.g_error_tv)
  private TextView errorTv;


  public interface IReLoadListener {

    void reload();
  }

  public ErrorView(Activity act) {
    ViewUtils.autoInjectAllField(this, act);
    AutoUtil.auto(act);
    super.init(act);
  }

  public void setLogoVisible(boolean visible) {
    ViewUtils.setVisible(logoIv, visible);
  }

  public ErrorView(Activity act, View view) {
    ViewUtils.autoInjectAllField(this, view);
    AutoUtil.auto(view);
    super.init(act);
  }

  public void setBackgroundColor(Context context, int codor) {
    ViewUtils.setBackgroundColor(context, layout, codor);
  }

  public void showError(View dataView, String msg, final IReLoadListener listener) {
    ViewUtils.setVisible(dataView, false);
    ViewUtils.setVisible(errorView, true);
    ViewUtils.setVisible(loadingTv, false);
    ViewUtils.setVisible(reLoadTv, null != listener);
    ViewUtils.setText(errorTv, Util.isNull(msg) ? ErrorUtil.NETERROR : msg);
    ViewUtils.setVisible(logoIv, true);
    ViewUtils.setClick(reLoadTv, v -> {
      if (listener != null) {
        listener.reload();
      }
    });

  }

  public void showError(View dataView, String msg, int errorCode, final IReLoadListener listener) {
    if (null != layout) {
      LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams) layout.getLayoutParams();
      ll.height = ScreenUtil.getHeight();
      ll.width = ScreenUtil.getWidth();
      layout.setLayoutParams(ll);
    }
    if (null != dataView) {
      dataView.setVisibility(View.GONE);
    }
    if (null != errorView) {
      errorView.setVisibility(View.VISIBLE);
    }
    if (null != errorTv) {
      errorTv.setText(Util.isNull(msg) ? ErrorUtil.NETERROR : msg);
    }
    if (null != loadingTv) {
      loadingTv.setVisibility(View.GONE);
    }
    if (null != reLoadTv) {
      reLoadTv.setVisibility(null != listener ? View.VISIBLE : View.GONE);
    }
    if (null != reLoadTv) {
      reLoadTv.setOnClickListener(v -> {
        if (listener != null) {
          listener.reload();
        }
      });
    }
    showErrorImage(errorCode);

  }

  public void showError(View dataView, String msg, int errorCode, int backgrounColor,
      final IReLoadListener listener) {
    setErrorView(dataView, listener);
    ViewUtils.setBackgroundColor(context, layout, backgrounColor);
    ViewUtils.setText(errorTv, Util.isNull(msg) ? ErrorUtil.NETERROR : msg);
    showErrorImage(errorCode);

  }

  private void setErrorView(View dataView, IReLoadListener listener) {
    if (null != layout) {
      LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams) layout.getLayoutParams();
      ll.height = ScreenUtil.getHeight();
      ll.width = ScreenUtil.getWidth();
      layout.setLayoutParams(ll);
    }
    ViewUtils.setVisible(dataView, false);
    ViewUtils.setVisible(errorView, true);
    ViewUtils.setVisible(loadingTv, false);
    ViewUtils.setVisible(reLoadTv, null != listener);
    ViewUtils.setVisible(logoIv, true);
    ViewUtils.setClick(reLoadTv, v -> {
      if (listener != null) {
        listener.reload();
      }
    });
  }

  public void showError(View dataView, int errorCode, final IReLoadListener listener) {
    if (null != dataView) {
      dataView.setVisibility(View.GONE);
    }
    if (null != errorView) {
      errorView.setVisibility(View.VISIBLE);
    }
    if (null != loadingTv) {
      loadingTv.setVisibility(View.GONE);
    }
    if (null != reLoadTv) {
      reLoadTv.setVisibility(null != listener ? View.VISIBLE : View.GONE);
    }
    if (null != reLoadTv) {
      reLoadTv.setOnClickListener(v -> {
        if (listener != null) {
          listener.reload();
        }
      });
    }
    showErrorImage(errorCode);
    showErrorMsg(errorCode);

  }

  private void showErrorMsg(int errorCode) {
    if (null == errorTv) {
      return;
    }
    switch (errorCode) {
      case ErrorUtil.NETERROR_CODE:
        errorTv.setText(ErrorUtil.NETERROR);
        break;
      case ErrorUtil.NONET_CODE:
        errorTv.setText(ErrorUtil.NONET);
        break;
      case ErrorUtil.NODATA_CODE:
        errorTv.setText(ErrorUtil.NODATA);
        break;
      case ErrorUtil.COUPON_CODE:
        errorTv.setText(ErrorUtil.NOCOUPON);
        break;
      case ErrorUtil.ORDER_CODE:
        errorTv.setText(ErrorUtil.NOORDER);
        break;
      default:
        errorTv.setText(ErrorUtil.NETERROR);
        break;

    }
    errorTv.setVisibility(View.VISIBLE);
  }

  private void showErrorImage(int errorCode) {
    if (null == logoIv) {
      return;
    }
    switch (errorCode) {
      case ErrorUtil.NETERROR_CODE:
        logoIv.setImageResource(R.mipmap.queseng_data);
        break;
      case ErrorUtil.NONET_CODE:
        logoIv.setImageResource(R.mipmap.queseng_wifi);
        break;
      case ErrorUtil.NODATA_CODE:
        logoIv.setImageResource(R.mipmap.queseng_data);
        break;
      case ErrorUtil.COUPON_CODE:
        logoIv.setImageResource(R.mipmap.queseng_coupons);
        break;
      case ErrorUtil.ORDER_CODE:
        logoIv.setImageResource(R.mipmap.queseng_order);
        break;
      default:
        break;
    }
    logoIv.setVisibility(View.VISIBLE);
  }

  public static int getLogoRes(int errorCode) {
    int res = R.mipmap.queseng_data;
    switch (errorCode) {
      case ErrorUtil.NETERROR_CODE:
      case ErrorUtil.NODATA_CODE:
        res = R.mipmap.queseng_data;
        break;
      case ErrorUtil.NONET_CODE:
        res = R.mipmap.queseng_wifi;
        break;
      case ErrorUtil.COUPON_CODE:
        res = R.mipmap.queseng_coupons;
        break;
      case ErrorUtil.ORDER_CODE:
        res = R.mipmap.queseng_order;
        break;
      case ErrorUtil.MESSAGE_CODE:
        res = R.mipmap.queseng_message;
        break;
      default:
        break;
    }
    return res;
  }

  public TextView getLoadingTv() {
    return loadingTv;
  }

  public void showData(View dataView) {
    if (null != dataView) {
      dataView.setVisibility(View.VISIBLE);
    }
    if (null != errorView) {
      errorView.setVisibility(View.GONE);
    }
  }

  public void hide() {
    if (null != errorView) {
      errorView.setVisibility(View.GONE);
    }
  }

  public void showLoading(View dataView, boolean showText) {
    if (null != dataView) {
      dataView.setVisibility(View.GONE);
    }
    if (null != errorView) {
      errorView.setVisibility(View.VISIBLE);
    }
    AnimaUtil.showLoading(context, showText ? loadingTv : null);
  }

  public void stopLoading(View dataView) {
    dataView.setVisibility(View.VISIBLE);
    AnimaUtil.stopLoading();
    hide();
  }

  public void stopLoading() {
    AnimaUtil.stopLoading();
    hide();
  }


}
